package com.seezoon.infrastructure.configuration;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;


/**
 * Accept-Language: en-US
 * Accept-Language: zh_CN
 */
@Configuration
public class I18nConfiguration {

    private final static int CACHE_SECONDS = 3600;
    private final static String BASE_NAME = "classpath:i18n/messages";


    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource =
                new ReloadableResourceBundleMessageSource();
        messageSource.setBasename(BASE_NAME);
        messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
        messageSource.setCacheSeconds(CACHE_SECONDS);
        return messageSource;
    }

    @Bean
    public LocaleResolver localeResolver() {
        AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
        resolver.setDefaultLocale(Locale.US);
        ArrayList<Locale> locales = new ArrayList<>();
        locales.add(Locale.US);
        locales.add(Locale.SIMPLIFIED_CHINESE);
        resolver.setSupportedLocales(locales);
        return resolver;
    }
}
